<?php

namespace App\Http\Middleware;

use Closure;
use App\Http\Classes\User;
use App\Http\Classes\Common;

class VerifyUserToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ( isset($_COOKIE['USER_ID']) && isset($_COOKIE['USER_TOKEN']) )
        {
            $user = new User( $_COOKIE['USER_ID'] );
            $result = $user->tokenVerify($_COOKIE['USER_TOKEN']);
            if ( $result )
            {
                return $next($request);
            }
            else
            {
                return Common::response(-1, '令牌过期，请重新登录', null, $user->getMessage());
            }
        }
        else
        {
            return Common::response(-1, '令牌过期，请重新登录', null);
        }
    }
}
